package com.cema.manage.invoke;

import com.cema.manage.invoke.interfase.TestJSONDataUtilsInterFase;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;


public class TestJSONDataUtils implements TestJSONDataUtilsInterFase {
    private final String CODE = "code";
    private final String DATALIST = "datalist";
    private final String TIMETAMP = "timetamp";
    // 成功 原生安卓
    public static final int SUCCESS_CODE = 0x000000;

    // 成功 返回类型 对象，集合
    public static final int SUCCESS_CODE1 = 0x000001;
    // 成功 字符串
    public static final int SUCCESS_CODE2 = 0x000002;
    // 失败 错误信息
    public static final int FAIL_CODE = 0x999999;

    //失败 接口未授权，token失效登录过期
    public static final int FAIL_CODE1 = 0x199999;

    private static TestJSONDataUtils jsonDataUtils;

    public static TestJSONDataUtils getInstance() {
        if (jsonDataUtils == null) {
            jsonDataUtils = new TestJSONDataUtils();
        }
        return jsonDataUtils;
    }

    private TestJSONDataUtils() {
    }

    @Override
    public JSONObject getJsonObject(Object objectData, String[] filterContent,
                                    int code) {
        JSONObject josnObject = new JSONObject();
        josnObject.put(CODE, code);
        switch (code) {
            case SUCCESS_CODE:
            case FAIL_CODE1:
            case SUCCESS_CODE2:
            case FAIL_CODE:
                josnObject.put(DATALIST, objectData);
                break;
            case SUCCESS_CODE1:
                JsonConfig config = new JsonConfig();
                JSONArray json = JSONArray.fromObject(objectData, config);
                josnObject.put(DATALIST, json);
                break;

        }
        josnObject.put(TIMETAMP, System.currentTimeMillis());
        return josnObject;
    }
}
